﻿using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ZoneLine.Views
{
    /// <summary>
    /// BusyLoding.xaml 的交互逻辑
    /// </summary>
    public partial class BusyLoding : UserControl
    {
        public BusyLoding()
        {
            InitializeComponent();
        }

        private static readonly DependencyProperty IsBusyProperty = DependencyProperty.Register("IsBusy", typeof(bool), typeof(BusyLoding), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, IsBusyPropertyChanged));

        private static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(BusyLoding), new FrameworkPropertyMetadata("正在努力加载中", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(TextPropertyChanged)));

        private static readonly DependencyProperty BaseGridProperty = DependencyProperty.Register("BaseGrid", typeof(Grid), typeof(BusyLoding));

        private static readonly DependencyProperty SuccessTextProperty = DependencyProperty.Register("SuccessText", typeof(string), typeof(BusyLoding), new FrameworkPropertyMetadata("操作成功", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(SuccessTextPropertyChanged)));

        private static void SuccessTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var loading = d as BusyLoding;
            if (loading != null)
            {
                loading.SuccessTb.Text = e.NewValue?.ToString();
            }
        }

        private static readonly DependencyProperty ErrTextProperty = DependencyProperty.Register("ErrText", typeof(string), typeof(BusyLoding), new FrameworkPropertyMetadata("操作失败", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(ErrTextPropertyChanged)));

        private static void ErrTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            BusyLoding bi = d as BusyLoding;
            if (bi != null)
            {
                bi.ErrTb.Text = e.NewValue?.ToString();
            }
        }

        public bool ActionSuccess
        {
            get { return (bool)GetValue(ActionSuccessProperty); }
            set
            {
                SetValue(ActionSuccessProperty, value);
                if (value)
                {
                    Application.Current.Dispatcher.Invoke(async () =>
                    {
                        if (string.IsNullOrEmpty(SuccessText))
                        {
                            SuccessText = "操作成功";
                        }
                        SuccessTb.Visibility = Visibility.Visible;
                        LoadHidden();
                        await Task.Delay(1000);
                        IsBusy = false;
                        SuccessTb.Visibility = Visibility.Collapsed;
                        ActionSuccess = false;
                        SuccessText = string.Empty;
                        ErrText = string.Empty;

                    });
                }
            }
        }

        public static readonly DependencyProperty ActionSuccessProperty =
            DependencyProperty.Register("ActionSuccess", typeof(bool), typeof(BusyLoding), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, ActionSuccessPropertyChanged));



        public bool ActionFailed
        {
            get { return (bool)GetValue(ActionFailedProperty); }
            set
            {
                SetValue(ActionFailedProperty, value);
                if (value)
                {
                    Application.Current.Dispatcher.Invoke(async () =>
                    {
                        if (string.IsNullOrEmpty(ErrText))
                        {
                            ErrText = "操作失败";
                        }
                        ErrTb.Visibility = Visibility.Visible;
                        LoadHidden();
                        await Task.Delay(1000);
                        IsBusy = false;
                        ErrTb.Visibility = Visibility.Collapsed;
                        ActionFailed = false;
                        SuccessText = string.Empty;
                        ErrText = string.Empty;
                    });
                }
            }
        }

        public static readonly DependencyProperty ActionFailedProperty =
            DependencyProperty.Register("ActionFailed", typeof(bool), typeof(BusyLoding), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, ActionFailedPropertyChanged));

        private static void IsBusyPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            BusyLoding bi = sender as BusyLoding;
            if (bi != null)
            {
                bi.OnIsBusyPropertyChanged(e.OldValue, e.NewValue);
            }
        }

        private static void ActionSuccessPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            BusyLoding bi = sender as BusyLoding;
            if (bi != null)
            {
                bi.OnActionSuccessPropertyChanged(e.OldValue, e.NewValue);
            }
        }
        private static void ActionFailedPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            BusyLoding bi = sender as BusyLoding;
            if (bi != null)
            {
                bi.OnActionFailedPropertyChanged(e.OldValue, e.NewValue);
            }
        }

        public Grid BaseGrid
        {
            get
            {
                return (Grid)this.GetValue(BaseGridProperty);
            }
            set
            {
                this.SetValue(BaseGridProperty, value);
            }
        }

        private static void TextPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            BusyLoding bi = sender as BusyLoding;
            if (bi != null)
            {
                bi.OnTextPropertyChanged(e.OldValue, e.NewValue);
            }
        }


        /// <summary>
        /// IsBusyProperty须双向绑定
        /// </summary>
        public bool IsBusy
        {
            get
            {
                return (bool)this.GetValue(IsBusyProperty);
            }
            set
            {
                this.SetValue(IsBusyProperty, value);
                if (value)
                {
                    this.Dispatcher.Invoke(() =>
                    {
                        if (BaseGrid != null)
                        {
                            BaseGrid.IsEnabled = false;
                            BaseGrid.Opacity = 0.2;
                        }
                        LoadShow();
                    });
                }
                else
                {
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        if (BaseGrid != null)
                        {
                            BaseGrid.IsEnabled = true;
                            BaseGrid.Opacity = 1;
                        }
                        LoadHidden();
                        SuccessText = string.Empty;
                        ErrText = string.Empty;
                    });
                }
            }
        }

        private void LoadHidden()
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                if (LoadingTb != null)
                {
                    LoadingTb.Visibility = Visibility.Collapsed;
                }
                Loading.Visibility = Visibility.Collapsed;
            });

        }

        private void LoadShow()
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                lock (this)
                {
                    if (LoadingTb != null)
                    {
                        LoadingTb.Visibility = Visibility.Visible;
                    }
                    Loading.Visibility = Visibility.Visible;
                }
            });
        }
        public string Text
        {
            get
            {
                return this.GetValue(TextProperty).ToString();
            }
            set
            {
                this.SetValue(TextProperty, value);
                if (LoadingTb != null)
                {
                    this.Dispatcher.Invoke(() => { LoadingTb.Text = value; });
                }
            }
        }

        public string SuccessText
        {
            get
            {
                return this.GetValue(SuccessTextProperty)?.ToString();
            }
            set
            {
                this.SetValue(SuccessTextProperty, value);
                if (SuccessTb != null)
                {
                    this.Dispatcher.Invoke(() => { SuccessTb.Text = value; });
                }
            }
        }

        public string ErrText
        {
            get
            {
                return this.GetValue(ErrTextProperty)?.ToString();
            }
            set
            {
                this.SetValue(ErrTextProperty, value);
                if (ErrTb != null)
                {
                    this.Dispatcher.Invoke(() => { ErrTb.Text = value; });
                }
            }
        }

        protected void OnIsBusyPropertyChanged(object oldValue, object newValue)
        {
            this.IsBusy = (bool)newValue;
        }

        protected void OnTextPropertyChanged(object oldValue, object newValue)
        {
            this.Text = newValue?.ToString();
        }

        protected void OnActionSuccessPropertyChanged(object oldValue, object newValue)
        {
            this.ActionSuccess = (bool)newValue;
        }
        protected void OnActionFailedPropertyChanged(object oldValue, object newValue)
        {
            this.ActionFailed = (bool)newValue;
        }

    }
}
